// pages/shop/shop_car/shop_car.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopLists:[],
    totalPrice:0,
    totalNum:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
  },

  stepperChange(e) {
    //e.detail.value 改变后的值
    //e.detail.type 操作的类型（输入，增加按钮，减少按钮）
    //e.detail.index 当前组件的索引
    //根据项目逻辑 自行处理
    let index = e.detail.index;
    let shopLists = this.data.shopLists;
    shopLists[index].num = e.detail.value;
    wx.setStorageSync('cars', shopLists)
    this.setData({
      shopLists
    })
    this.checkTotalPrice()
  },
  checkTotalPrice() { //统计金额，数量
    let totalPrice = 0;
    let totalNum = 0;
    let shopLists = this.data.shopLists;
    shopLists.map(item => {
      if(item.selected){
        totalPrice = item.price * item.num + totalPrice;
        totalNum = item.num + totalNum;
      }
    })
    this.setData({
      totalPrice,
      totalNum
    })
  },
  selectGoods(e){
    let index = e.currentTarget.dataset.index;
    let shopLists = this.data.shopLists;
    shopLists[index].selected = !shopLists[index].selected;
    this.setData({
      shopLists
    })
    wx.setStorageSync('cars', shopLists)
    this.checkTotalPrice()
  },
  submit(){
    let newShopLists = [];
    let shopLists = this.data.shopLists;
    shopLists.map(item=>{
      if(item.selected){
        newShopLists.push(item)
      }
    })
    if(newShopLists.length == 0){
      wx.showToast({
        title: '请选择结算商品',
        icon:'none'
      })
      return false;
    }
    wx.navigateTo({
      url: '/pages/shop/create_order/create_order',
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let cars = wx.getStorageSync('cars');
    if(cars){
      this.setData({
        shopLists:cars
      })
      this.checkTotalPrice()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})